mod common;
use crate::common::*;
use crate::psn::ck_psn;
use crate::station::ck_station;
use crate::task::{ck_task, task_data};
use crate::tech::{ck_tech, tech_data};
use base::base::VMap;
use serde_json::json;

#[tokio::test]
async fn test_rmpsn_1() {
    let tech_val = tech_data();
    let val = task_data();

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "M010",
                "name": "name_M010",
            }
        }),
        "station",
        "add",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "M020",
                "name": "name_M020",
            }
        }),
        "station",
        "add",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "v": ["psn1", "psn2"]
        }),
        "station",
        "addpsn",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "rework": true,
            "v": ["psn1", "psn2"]
        }),
        "station",
        "addpsn",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M020",
            "v": ["psn1", "psn2"]
        }),
        "station",
        "addpsn",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "rework": true,
            "k": "M020",
            "v": ["psn1", "psn2"]
        }),
        "station",
        "addpsn",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "te1",
                "name": "name_te1",
            }
        }),
        "tech",
        "add",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": val.ck_val("task1").unwrap()
        }),
        "task",
        "add",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
            "v": [{"sn": "psn1"}, {"sn": "psn2"}],
        }),
        "psn",
        "add",
    )
    .await;

    ck_tech(
        pos!(),
        json!({
            "all": tech_val.ck_val("all").unwrap(),
            "year": tech_val.ck_val("year_1").unwrap(),
            "te1": tech_val.ck_val("te1").unwrap(),
        }),
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "run": json!([
                val.ck_val("run1").unwrap(),
            ]),
            "all": val.ck_val("y4").unwrap(),
            "y4": val.ck_val("m1").unwrap(),
            "m41": val.ck_val("d1").unwrap(),
            "d411": val.ck_val("t1").unwrap(),
            "task1": val.ck_val("task1").unwrap(),
        }),
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010", "M020"],
            "M010": {"name": "name_M010", "sn": "M010"},
            "M020": {"name": "name_M020", "sn": "M020"},
            "psn1": ["psn1", "psn2"],
            "psn2": ["psn1", "psn2"],
            "rework1": ["psn1", "psn2"],
            "rework2": ["psn1", "psn2"],
        }),
    )
    .await;

    ck_psn(
        pos!(),
        json!({
            "task": ["psn1", "psn2"],
            "psn1": {"sn": "psn1", "task": "task1"},
            "psn2": {"sn": "psn2", "task": "task1"},
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
        }),
        "task",
        "rm",
    )
    .await;

    ck_tech(
        pos!(),
        json!({
            "all": tech_val.ck_val("all").unwrap(),
            "year": tech_val.ck_val("year_1").unwrap(),
            "te1": tech_val.ck_val("te1").unwrap(),
        }),
    )
    .await;
    ck_task(pos!(), json!({})).await;
    ck_station(
        pos!(),
        json!({
            "all": ["M010", "M020"],
            "M010": {"name": "name_M010", "sn": "M010"},
            "M020": {"name": "name_M020", "sn": "M020"},
        }),
    )
    .await;
    ck_psn(pos!(), json!({})).await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "te1",
        }),
        "tech",
        "rm",
    )
    .await;

    ck_tech(pos!(), json!({})).await;
    ck_task(pos!(), json!({})).await;
    ck_psn(pos!(), json!({})).await;
    ck_station(
        pos!(),
        json!({
            "all": ["M010", "M020"],
            "M010": {"name": "name_M010", "sn": "M010"},
            "M020": {"name": "name_M020", "sn": "M020"},
        }),
    )
    .await;
}

#[tokio::test]
async fn test_rmpsn_2() {
    let tech_val = tech_data();
    let val = task_data();

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "M010",
                "name": "name_M010",
            }
        }),
        "station",
        "add",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "M020",
                "name": "name_M020",
            }
        }),
        "station",
        "add",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "v": ["psn1", "psn2"]
        }),
        "station",
        "addpsn",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M010",
            "rework": true,
            "v": ["psn1", "psn2"]
        }),
        "station",
        "addpsn",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "M020",
            "v": ["psn1", "psn2"]
        }),
        "station",
        "addpsn",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "rework": true,
            "k": "M020",
            "v": ["psn1", "psn2"]
        }),
        "station",
        "addpsn",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": {
                "sn": "te1",
                "name": "name_te1",
            }
        }),
        "tech",
        "add",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": val.ck_val("task1").unwrap()
        }),
        "task",
        "add",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "v": val.ck_val("task2").unwrap()
        }),
        "task",
        "add",
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "task1",
            "v": [{"sn": "psn1"}, {"sn": "psn2"}],
        }),
        "psn",
        "add",
    )
    .await;

    ck_tech(
        pos!(),
        json!({
            "all": tech_val.ck_val("all").unwrap(),
            "year": tech_val.ck_val("year_1").unwrap(),
            "te1": tech_val.ck_val("te1").unwrap(),
        }),
    )
    .await;

    ck_task(
        pos!(),
        json!({
            "run": json!([
                val.ck_val("run1").unwrap(),
                val.ck_val("run2").unwrap(),
            ]),
            "all": val.ck_val("y4").unwrap(),
            "y4": val.ck_val("m1").unwrap(),
            "m41": val.ck_val("d1").unwrap(),
            "d411": val.ck_val("t").unwrap(),
            "task1": val.ck_val("task1").unwrap(),
            "task2": val.ck_val("task2").unwrap(),
        }),
    )
    .await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010", "M020"],
            "M010": {"name": "name_M010", "sn": "M010"},
            "M020": {"name": "name_M020", "sn": "M020"},
            "psn1": ["psn1", "psn2"],
            "psn2": ["psn1", "psn2"],
            "rework1": ["psn1", "psn2"],
            "rework2": ["psn1", "psn2"],
        }),
    )
    .await;

    ck_psn(
        pos!(),
        json!({
            "task": ["psn1", "psn2"],
            "psn1": {"sn": "psn1", "task": "task1"},
            "psn2": {"sn": "psn2", "task": "task1"},
        }),
    )
    .await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "2024-01-01",
        }),
        "task",
        "rmonth",
    )
    .await;

    ck_tech(
        pos!(),
        json!({
            "all": tech_val.ck_val("all").unwrap(),
            "year": tech_val.ck_val("year_1").unwrap(),
            "te1": tech_val.ck_val("te1").unwrap(),
        }),
    )
    .await;

    ck_task(pos!(), json!({})).await;

    ck_station(
        pos!(),
        json!({
            "all": ["M010", "M020"],
            "M010": {"name": "name_M010", "sn": "M010"},
            "M020": {"name": "name_M020", "sn": "M020"},
        }),
    )
    .await;

    ck_psn(pos!(), json!({})).await;

    ok(
        pos!(),
        json!({
            "line": "1",
            "k": "te1",
        }),
        "tech",
        "rm",
    )
    .await;

    ck_tech(pos!(), json!({})).await;
}
